﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCCN.DAL;
using SCCN.BLL;
using SCCN.Utility;

public partial class Admin_AddArticle : ASPBaseClass
{
    ArticleBLL bllArticle = ArticleBLL.getInstance();
    CategoryBLL bllCategory = CategoryBLL.getInstance();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDrp();
            if (HttpHelper.GetParameters("id", HttpHelper.MethodType.GET).Length > 0)
            {
                Bind();
            }
        }
    }

    void Bind()
    {
        int id = 0;
        int.TryParse(HttpHelper.GetParameters("id", HttpHelper.MethodType.GET), out id);
        SC_Article obj = bllArticle.GetSC_ArticleById(id);
        this.txtContent.Text = HttpUtility.HtmlDecode(obj.ArticleContent);
        this.txtKeyWord.Text = obj.KeyWord;
        this.txtTag.Text = obj.Tag;
        this.txtTitle.Text = obj.ArticleTitle;
        this.drpCategoryName.SelectedValue = obj.CategoryId.ToString();
        this.drpIsPicCover.SelectedValue = obj.IsPicCover == true ? "1" : "0";
        this.drpIsTag.SelectedValue = obj.IsTag == true ? "1" : "0";
        this.drpIsHot.SelectedValue = obj.IsHot == true ? "1" : "0";
        this.drpIsChoice.SelectedValue = obj.IsChoice == true ? "1" : "0";
        this.lblPicCoverUrl1.Text = obj.PicCoverUrl1;
        this.lblPicCoverUrl2.Text = obj.PicCoverUrl2;
        this.lblPicCoverUrl3.Text = obj.PicCoverUrl3;
        this.lblPicCoverUrl4.Text = obj.PicCoverUrl4;
        this.lblPicCoverUrl5.Text = obj.PicCoverUrl5;
        this.hid.Value = obj.Id.ToString();
    }

    void BindDrp()
    {
        this.drpCategoryName.DataSource = bllCategory.GetCategory();
        this.drpCategoryName.DataTextField = "CategoryName";
        this.drpCategoryName.DataValueField = "Id";
        this.drpCategoryName.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (this.fup1.HasFile)
        {
            try
            {
                string FileName = fup1.PostedFile.FileName;
                string upload_file = Server.MapPath("~/") + "upload/article/" + FileName;
                fup1.PostedFile.SaveAs(upload_file);//开始上传文件
                this.lblPicCoverUrl1.Text = "/upload/article/" + FileName;
            }
            catch (Exception ex)
            {
                HttpHelper.Alert("上传未成功！", Page);
            }
        }
        if (this.fup2.HasFile)
        {
            try
            {
                string FileName = fup2.PostedFile.FileName;
                string upload_file = Server.MapPath("~/") + "upload/article/" + FileName;
                fup2.PostedFile.SaveAs(upload_file);//开始上传文件
                this.lblPicCoverUrl2.Text = "/upload/article/" + FileName;
            }
            catch (Exception ex)
            {
                HttpHelper.Alert("上传未成功！", Page);
            }
        }
        if (this.fup3.HasFile)
        {
            try
            {
                string FileName = fup3.PostedFile.FileName;
                string upload_file = Server.MapPath("~/") + "upload/article/" + FileName;
                fup3.PostedFile.SaveAs(upload_file);//开始上传文件
                this.lblPicCoverUrl3.Text = "/upload/article/" + FileName;
            }
            catch (Exception ex)
            {
                HttpHelper.Alert("上传未成功！", Page);
            }
        }
        if (this.fup4.HasFile)
        {
            try
            {
                string FileName = fup4.PostedFile.FileName;
                string upload_file = Server.MapPath("~/") + "upload/article/" + FileName;
                fup4.PostedFile.SaveAs(upload_file);//开始上传文件
                this.lblPicCoverUrl4.Text = "/upload/article/" + FileName;
            }
            catch (Exception ex)
            {
                HttpHelper.Alert("上传未成功！", Page);
            }
        }
        if (this.fup5.HasFile)
        {
            try
            {
                string FileName = fup5.PostedFile.FileName;
                string upload_file = Server.MapPath("~/") + "upload/article/" + FileName;
                fup5.PostedFile.SaveAs(upload_file);//开始上传文件
                this.lblPicCoverUrl5.Text = "/upload/article/" + FileName;
            }
            catch (Exception ex)
            {
                HttpHelper.Alert("上传未成功！", Page);
            }
        }
        if (this.hid.Value.Length == 0)
        {
            SC_Article obj = new SC_Article();
            obj.ArticleTitle = this.txtTitle.Text;
            obj.CategoryId = Convert.ToInt32(this.drpCategoryName.SelectedValue);
            obj.ArticleContent = HttpUtility.HtmlEncode(this.txtContent.Text);
            obj.CreateDate = DateTime.Now;
            obj.IsDelete = false;
            obj.IsPass = false;
            obj.Tag = this.txtTag.Text;
            obj.KeyWord = this.txtKeyWord.Text;
            obj.PublishPeopleId = UserId;
            obj.IsPicCover = this.drpIsPicCover.SelectedValue == "1" ? true : false;
            obj.PicCoverUrl1 = this.lblPicCoverUrl1.Text;
            obj.PicCoverUrl2 = this.lblPicCoverUrl2.Text;
            obj.PicCoverUrl3 = this.lblPicCoverUrl3.Text;
            obj.PicCoverUrl4 = this.lblPicCoverUrl4.Text;
            obj.PicCoverUrl5 = this.lblPicCoverUrl5.Text;
            obj.CreateUrl = string.Empty;
            obj.IsChoice = this.drpIsChoice.SelectedValue == "1" ? true : false;
            obj.IsHot = this.drpIsHot.SelectedValue == "1" ? true : false;
            obj.IsTag = this.drpIsTag.SelectedValue == "1" ? true : false;
            if (bllArticle.AddArticle(obj))
            {
                HttpHelper.Alert("保存成功！", Page);
            }
            else
            {
                HttpHelper.Alert("保存失败！", Page);
            }
        }
        else
        {
            SC_Article obj = bllArticle.GetSC_ArticleById(Convert.ToInt32(this.hid.Value));
            obj.ArticleTitle = this.txtTitle.Text;
            obj.CategoryId = Convert.ToInt32(this.drpCategoryName.SelectedValue);
            obj.ArticleContent = HttpUtility.HtmlEncode(this.txtContent.Text);
            obj.CreateDate = DateTime.Now;
            obj.IsDelete = false;
            obj.IsPass = false;
            obj.KeyWord = this.txtKeyWord.Text;
            obj.PublishPeopleId = UserId;
            obj.Tag = this.txtTag.Text;
            obj.IsPicCover = this.drpIsPicCover.SelectedValue == "1" ? true : false;
            obj.PicCoverUrl1 = this.lblPicCoverUrl1.Text;
            obj.PicCoverUrl2 = this.lblPicCoverUrl2.Text;
            obj.PicCoverUrl3 = this.lblPicCoverUrl3.Text;
            obj.PicCoverUrl4 = this.lblPicCoverUrl4.Text;
            obj.PicCoverUrl5 = this.lblPicCoverUrl5.Text;
            obj.IsChoice = this.drpIsChoice.SelectedValue == "1" ? true : false;
            obj.IsHot = this.drpIsHot.SelectedValue == "1" ? true : false;
            obj.IsTag = this.drpIsTag.SelectedValue == "1" ? true : false;
            if (bllArticle.Update(obj))
            {
                HttpHelper.Alert("保存成功！", Page);
            }
            else
            {
                HttpHelper.Alert("保存失败！", Page);
            }
        }
    }

    void CreateArticle()
    {

    }


    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("ArticleList.aspx?categoryid=" + this.drpCategoryName.SelectedValue);
    }
}